package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.RecordMapper;
import com.bbt.dormassistant.dao.UserInformationMapper;
import com.bbt.dormassistant.entity.Record;
import com.bbt.dormassistant.service.RecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecordServiceImpl implements RecordService {


    @Resource
    private RecordMapper recordMapper;

    @Resource
    private UserInformationMapper userInformationMapper;

    /**
     /* *
     *
     * @param record 房间ID
     * @return Boolean
     */
    @Override
    public Boolean addRecord(Record record) {

        try {
            return recordMapper.insert(record) == 1;
        }catch (Exception e ){
            e.printStackTrace();
            return false;
        }
    }


    /**
     * \
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<Record>
     */
    @Override
    public List<Record> queryAll(Integer currIndex, Integer pageSize) {
        Map<String ,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize==null? 100:pageSize);
        return recordMapper.selectAll(data);
    }

    @Override
    public Record getRecordByRecordId(Integer recordId) {
        return recordMapper.selectByPrimaryKey(recordId);
    }

    @Override
    public Boolean updateRecord(Record record) {
        return recordMapper.updateByPrimaryKey(record) == 1;
    }


    /**
     *
     * @param userId 用户ID
     * @param currIndex 当前页码
     * @param pageSize 页大小
     * @return List<Record>
     */
    @Override
    public List<Record> getRecordByUserId(String userId,Integer currIndex,Integer pageSize) {
        Map<String, Object> data = new HashMap<>();
        data.put("userId",userId);
        data.put("currIndex",currIndex == null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize == null? 100:pageSize);
        return recordMapper.selectAllByUserId(data);
    }



    /**
     *
     * @param roomId 根据房间ID返回所有记录
     * @param currIndex 当前页码
     * @param pageSize 页大小
     * @return List<Record>
     */
    @Override
    public List<Record> getRecordByRoomId(Integer roomId,Integer currIndex,Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("roomId",roomId);
        data.put("currIndex",currIndex == null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize == null? 100:pageSize);
        return recordMapper.selectAllByRoomId(data);
    }






}
